package com.myway.dto.tcscenery.support;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class Theader {

	private Integer actionCode;
	
	private Integer rspType;
	
	private String rspCode;
	
	private String rspDesc;
	
	private String rspTime;

	public Theader xmlToTheader(String xml){
		 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	     DocumentBuilder db = null;
	     Document doc = null;
	     Theader t = new Theader();
	     try {
			db = dbf.newDocumentBuilder();
			try {
				doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
				NodeList list = doc.getElementsByTagName("header"); 
				for(int i = 0; i < list.getLength(); i++)
			    { 
					 Element element = (Element)list.item(i);
					Integer actionCode = Integer.parseInt(element.getElementsByTagName("actionCode").item(0).getFirstChild().getNodeValue());
					t.setActionCode(actionCode);
					Integer rspType = Integer.parseInt(element.getElementsByTagName("rspType").item(0).getFirstChild().getNodeValue());
					t.setRspType(rspType);
					String rspCode = element.getElementsByTagName("rspCode").item(0).getFirstChild().getNodeValue();
					t.setRspCode(rspCode);
					String rspDesc = element.getElementsByTagName("rspDesc").item(0).getFirstChild().getNodeValue();
					t.setRspDesc(rspDesc);
					String rspTime = element.getElementsByTagName("rspTime").item(0).getFirstChild().getNodeValue();
			        t.setRspTime(rspTime);
			    }
				
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return t;
	}
	public Integer getActionCode() {
		return actionCode;
	}

	public void setActionCode(Integer actionCode) {
		this.actionCode = actionCode;
	}

	public Integer getRspType() {
		return rspType;
	}

	public void setRspType(Integer rspType) {
		this.rspType = rspType;
	}

	public String getRspCode() {
		return rspCode;
	}

	public void setRspCode(String rspCode) {
		this.rspCode = rspCode;
	}

	public String getRspDesc() {
		return rspDesc;
	}

	public void setRspDesc(String rspDesc) {
		this.rspDesc = rspDesc;
	}

	public String getRspTime() {
		return rspTime;
	}

	public void setRspTime(String rspTime) {
		this.rspTime = rspTime;
	}
	
}
